/**
 * This class is the controller for the main view for the application. It is specified as
 * the "controller" of the Main view class.
 */
Ext.define('Jason.view.main.MainController', {
    extend: 'Ext.app.ViewController',

    alias: 'controller.main',

    init : function(){
        var me = this;
        EUtil.initAccount();
        Ext.setGlyphFontFamily('FontAwesome'); //指定矢量图字体

        EUtil.initCfg();
        if(Ext.isIE8m){
            EUtil.showMsg({
                message : '为了您更好的体验，请使用chrome浏览器或Firefox浏览器'
            });
        }
        try{
            var treeList = me.getView().down('#navigationTreeList');
            if(treeList){
                treeList.getStore().removeAll();
                treeList.getStore().load();
            }
        }catch(e){}
        //重置变量

        Ext.Viewport = this.getView();
        /**

         * 禁用浏览器自带右键

         */
        // Ext.getDoc().on("contextmenu", function(e){  
        //     e.stopEvent();  
        // }); 
        //增加锁屏快捷键
        Ext.Viewport.down('#navigationTreeList').setBind({store:'{leftMenu}'});
    },
    listen : {
        controller : {
            '#' : {
                unmatchedroute : 'onRouteChange'
            }
        }
    },

    routes: {
        ':node': 'onRouteChange'
    },

    setCurrentView: function(hashTag) {
        hashTag = (hashTag || '');//.toLowerCase();

        var me = this,
            refs = me.getReferences(),
            mainCard = refs.mainCardPanel,
            // viewMainCard = mainCard.down('#contentPanel'),

            viewMainCard = mainCard,
            navigationList = refs.navigationTreeList,
            viewModel = me.getViewModel(),
            vmData = viewModel.getData(),
            store = navigationList.getStore(),
            node = store.findNode('routeId', hashTag),
            view = node ? node.get('view') : hashTag,//如果没有找到则使用原有的

            newView;
        //设置面包屑导航

        viewModel.set('selectedNodeByBreadcrumb',navigationList.getStore().findNode('extendtype',hashTag));

        if(hashTag === 'userCenter'){ /* 如果用户进入到是用户中心 */
            view = 'protal.authentication.UserCenter';
        }
        if(hashTag === 'dashboard'){
            view = 'protal.dashboard.Dashboard';
        }
        viewMainCard.setCurrentViewByPath(view);
        navigationList.setSelection(node);
        EUtil.destoryLoading();
    },
    /**

     * 点击树节点跳转页面

     * @param tree  输结构

     * @param node  点击节点

     */
    onNavigationTreeSelectionChange: function (tree, node) {
        var self = this;
        try{
            if (node && node.get('view')) {
                this.redirectTo(node.get("routeId"));
            }else{
                if(node.get('redirect')){
                    window.location.href = window.location.origin + window.location.pathname + '?' + node.get("routeId");
                }else{
                    // EUtil.toast('缺少路由配置',2);

                }
            }
        }catch(e){}
    },
    onRouteChange:function(id){
        var self = this;
        EUtil.showLoading('页面组件加载中...');
        // var store = self.getView().down('#navigationTreeList').getStore();
        Ext.defer(function(){
            self.setCurrentView(id);
        },800);
    }

});
